package com.interheart.framecore.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.NameFilter;
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.interheart.framecore.dto.EnumProDto;

/**
 * @Auther: SHIKAI
 * @Date: 2019/6/12 11:26
 */
public class JsonUtil {
    private final static String K_NAME = "id";//默认序列化时k的属性值是取ID
    private final static String V_NAME = "name";//默认序列化时v的属性值是取name

    private final static String ENUM_K_PRO = "value";//默认枚举转对应K的属性名
    private final static String ENUM_V_PRO = "desc";//默认枚举转对应V的属性名


    /**
     * 功能描述 获取KV序列化的过滤配置
     * @author SHIKAI
     * @date 2019/5/10
     * @param clazz 序列化对象class
     * @param SerId 序列化后K的名字
     * @param SerVal 序列化后V的名字
     * @param proId 序列化对象取K值的属性名字
     * @param proVal 序列化对象取V值的属性名字
     * @return
     */
    public static SerializeFilter[] serializeFilterToKv(Class<?> clazz, String serId, String serVal, String proId, String proVal ){
        SimplePropertyPreFilter simplePropertyPreFilter = new SimplePropertyPreFilter(clazz, proId, proVal);
        NameFilter nameFilter = (source, name, value) -> {
            if (name.equals(proId)){
                return serId;
            }
            if (name.equals(proVal)){
                return serVal;
            }
            return name;
        };

        return new SerializeFilter [] {simplePropertyPreFilter, nameFilter};
    }

    /**
     * 功能描述 获取KV序列化的过滤配置 啥都不传，默认序列化都是 id和name
     * @author SHIKAI
     * @date 2019/5/10
     * @return
     */
    public static SerializeFilter[] defaultKv(Class<?> clazz){
        return serializeFilterToKv(clazz, K_NAME, V_NAME, K_NAME, V_NAME);
    }

    /**
     * 功能描述 获取KV序列化的过滤配置 针对枚举 固定序列化属性
     * @author SHIKAI
     * @date 2019/5/10
     * @param clazz 序列化对象class
     * @return
     */
    public static SerializeFilter[] defaultEnumKv(Class<?> clazz){
        return serializeFilterToKv(clazz, K_NAME, V_NAME, ENUM_K_PRO, ENUM_V_PRO);
    }

    /**
     * 功能描述 获取KV序列化的过滤配置 默认K是id, 手动传入序列化和反序列化的v名称
     * @author SHIKAI
     * @date 2019/5/10
     * @param clazz 序列化对象class
     * @param kPro 序列化对象对应K的属性名
     * @param vPro 序列化对象对应V的属性名
     * @return
     */
    public static SerializeFilter[] appointProKv(Class<?> clazz, String kPro, String vPro){
        return serializeFilterToKv(clazz, K_NAME, V_NAME, kPro, vPro);
    }

    /**
     * 功能描述 把对象根据指定序列化过滤转化后返回
     * @author SHIKAI
     * @date 2019/7/22
     * @param  
     * @return 
     */
    public static Object parseSerializeFilterJSON(Object obj, SerializeFilter[] serializeFilter){
        String serializeFilterJSON = JSON.toJSONString(obj, serializeFilter);
        return JSON.parse(serializeFilterJSON);
    }
    /**
     * 功能描述 把对象根据指定序列化过滤转化后返回
     * @author SHIKAI
     * @date 2019/7/22
     * @param
     * @return
     */
    public static Object parseSerializeFilterEnumJSON(Class<?> enumClazz, SerializeFilter[] serializeFilter){
        String serializeFilterJSON = JSON.toJSONString(EnumUtil.toProList(enumClazz), serializeFilter);
        return JSON.parse(serializeFilterJSON);
    }
}
